<!---
Copyright: (c) 2006 The MachBlog Authors
Authors: Matt Woodward (mpwoodward@gmail.com) & Peter J. Farrell (pjf@maestropublishing.com)

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. 
You may obtain a copy of the License at 

http://www.apache.org/licenses/LICENSE-2.0 

Unless required by applicable law or agreed to in writing, software 
distributed under the License is distributed on an "AS IS" BASIS, 
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
See the License for the specific language governing permissions and 
limitations under the License.

$Id: UserListener.cfc 960 2006-10-26 05:16:16Z mpwoodward $

Notes:
This listener is autowired by ColdSpring.
--->
<cfcomponent
	displayname="UserListener"
	output="false"
	extends="MachII.framework.Listener" 
	hint="UserListener for MachBlog">

	<!---
	INITIALIZATION / CONFIGURATION
	--->
	<cffunction name="configure" access="public" returntype="void" output="false"
		hint="Configures the listener as part of the Mach-II framework.">
		<!--- Does nothing. --->
	</cffunction>

	<cffunction name="setUserService" access="public" returntype="void" output="false"
		hint="Dependency: injected">
		<cfargument name="userService" type="org.machblog.user.UserService" required="true" />
		<cfset variables.userService = arguments.userService />
	</cffunction>	
	<cffunction name="getUserService" access="private" returntype="org.machblog.user.UserService" output="false">
		<cfreturn variables.userService />
	</cffunction>
	
	<!---
	PUBLIC FUNCTIONS
	--->
	<cffunction name="getAllUsers" access="public" returntype="query" output="false" 
			hint="Retrieves all users in the system">
		<cfreturn getUserService().getAllUsers() />
	</cffunction>
	
	<cffunction name="getActiveUsers" access="public" output="false" returntype="query" 
			hint="Retrives active users in the system">
		<cfreturn getUserService().getActiveUsers() />
	</cffunction>
	
	<cffunction name="processUserForm" access="public" output="false" returntype="void" 
			hint="Processes the user form and announces the next event">
		<cfargument name="event" type="MachII.framework.Event" required="true" />
		
		<cfset var exitEvent = "showUserAdmin" />
		<cfset var user = arguments.event.getArg("user") />
		<cfset var results = StructNew() />
		<cfset results.success = true />
		
		<!--- handle additional info that couldn't be included in the event bean --->
		<cfif arguments.event.isArgDefined("isActive")>
			<cfset user.getAudit().setIsActive(arguments.event.getArg("isActive")) />
		</cfif>

		<cfif user.getUserId() EQ "">
			<cfset user.setUserId(CreateUUID()) />
			<cfset user.getAudit().setCreatedById(getUserService().getUserFacade().getUser().getUserId()) />
			<cfset user.getAudit().setDtCreated(getProperty("resourceBundleService").getLocaleUtils().toEpoch(Now())) />
			<cfset user.getAudit().setIpCreated(CGI.REMOTE_ADDR) />
			
			<cfset results.message = getProperty("resourceBundleService").getResourceBundle().getResource("usercreatesuccess") />
			
			<cftry>
				<cfset getUserService().createUser(user) />
				<cfcatch type="any">
					<cfset results.success = false />
					<cfset exitEvent = "showUserForm" />
					<cfif CFCATCH.Message EQ "Duplicate Email">
						<cfset results.message = getProperty("resourceBundleService").getResourceBundle().getResource("duplicateemail") />
					<cfelse>
						<cfset results.message = getProperty("resourceBundleService").getResourceBundle().getResource("usercreatefailed") />
					</cfif>
				</cfcatch>
			</cftry>
		<cfelse>
			<cfset user.getAudit().setModifiedById(getUserService().getUserFacade().getUser().getUserId()) />
			<cfset user.getAudit().setDtModified(getProperty("resourceBundleService").getLocaleUtils().toEpoch(Now())) />
			<cfset user.getAudit().setIpModified(CGI.REMOTE_ADDR) />
			
			<cfset results.message = getProperty("resourceBundleService").getResourceBundle().getResource("userupdatesuccess") />
			
			<cftry>
				<cfset getUserService().updateUser(user) />
				<cfcatch type="any">
					<cfset results.success = false />
					<cfset exitEvent = "showUserForm" />
					<cfif CFCATCH.Message EQ "Duplicate Email">
						<cfset results.message = getProperty("resourceBundleService").getResourceBundle().getResource("duplicateemail") />
					<cfelse>
						<cfset results.message = getProperty("resourceBundleService").getResourceBundle().getResource("userupdatefailed") />
					</cfif>
				</cfcatch>
			</cftry>
		</cfif>
		
		<!--- set the message and announce the next event --->
		<cfset arguments.event.setArg("message", results.message) />
		
		<cfset announceEvent(exitEvent, arguments.event.getArgs()) />
	</cffunction>
	
	<cffunction name="getUserById" access="public" returntype="org.machblog.user.User" output="false"
		hint="Retrieves a user bean.">
		<cfargument name="event" type="MachII.framework.Event" required="true" />
		
		<cfreturn getUserService().getUserById(arguments.event.getArg("userId", "")) />
	</cffunction>
	
	<cffunction name="createUser" access="public" returntype="void" output="false"
			hint="Creates a new user">
		<cfargument name="event" type="MachII.framework.Event" required="true" />

		<cfset var user = arguments.event.getArg("user") />

		<cfset getUserService().createUser(user) />
	</cffunction>
	
	<cffunction name="updateUser" access="public" returntype="void" output="false"
			hint="Updates a user">
		<cfargument name="event" type="MachII.framework.Event" required="true" />

		<cfset var user = arguments.event.getArg("user") />
		
		<cfset getUserService().updateUser(user) />
	</cffunction>
	
	<cffunction name="deleteUser" access="public" output="false" returntype="void" 
			hint="Deletes a user and announces the next event">
		<cfargument name="event" type="MachII.framework.Event" required="true" />
		
		<cfset var user = arguments.event.getArg("user") />
		<cfset var results = StructNew() />
		<cfset results.success = true />
		<cfset results.message = getProperty("resourceBundleService").getResourceBundle().getResource("userdeletesuccess") />
		
		<cftry>
			<cfset getUserService().deleteUser(user) />
			<cfcatch type="any">
				<cfset results.success = false />
				<cfset results.message = getProperty("resourceBundleService").getResourceBundle().getResource("userdeletefailed") />
			</cfcatch>
		</cftry>
		
		<cfset arguments.event.setArg("message", results.message) />
		
		<cfset announceEvent("showUserAdmin", arguments.event.getArgs()) />
	</cffunction>
	
	<cffunction name="toggleUserStatus" access="public" output="false" returntype="void" 
			hint="Toggles a user's status to either active or inactive based on current status and announces the next event">
		<cfargument name="event" type="MachII.framework.Event" required="true" />
		
		<cfset var user = getUserService().getUserBean() />
		<cfset var results = StructNew() />
		<cfset results.success = true />
		<cfset results.message = getProperty("resourceBundleService").getResourceBundle().getResource("userupdatesuccess") />
		
		<cfset user = getUserService().getUserById(arguments.event.getArg("userId")) />
		<cfset user.getAudit().setIsActive(NOT user.getAudit().getIsActive()) />
		<cfset user.getAudit().setModifiedById(getUserService().getUserFacade().getUser().getUserId()) />
		<cfset user.getAudit().setDtModified(getProperty("resourceBundleService").getLocaleUtils().toEpoch(Now()))>
		
		<cftry>
			<cfset getUserService().updateUser(user) />
			<cfcatch type="any">
				<cfset results.message = getProperty("resourceBundleService").getResourceBundle().getResource("userupdatefailed") />
			</cfcatch>
		</cftry>
		
		<cfset arguments.event.setArg("message", results.message) />
		<cfset announceEvent("showUserAdmin", arguments.event.getArgs()) />
	</cffunction>
</cfcomponent>
